package com.uni.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Uni
 * @create 2021/12/18 11:49
 */
public class DruidUtil {
    static DruidDataSource dataSource;
    static {
        Properties prop = new Properties();
        try{
            prop.load(DruidUtil.class.getClassLoader().getResourceAsStream("dbpool.properties"));
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        try{
            return dataSource.getConnection();
        } catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    public static void release(Connection connection, PreparedStatement pstmt, ResultSet rs){
        try {
            if (pstmt != null) pstmt.close();
            if (rs != null) rs.close();
            if (connection != null) connection.close();
        } catch (Exception e){
            e.printStackTrace();
        }
    }


}
